package itsm.isperp.codegenerator;

import itsm.isperp.framework.template.Template;
import itsm.isperp.framework.template.TemplateException;
import itsm.isperp.framework.utils.FileUtils;
import itsm.isperp.framework.utils.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 
 * 代码生成器
 * 
 * @author lizx
 * @date 2014-7-18
 * @since 1.0
 * 
 */
public class Generator {

	String tablename = "";
	String module = "";
	String mName = "";
	String title = "";
	String entityName = "";
	String entityName2 = "";
	String tplPath;
	String dbName;
	Map<String, Object> map;

	@Test
	public void generator() throws Exception {
		dbName = "isperp_hnhc";
		tablename = "qj_feiyong";
		module = "qingjia";
		title = "请假费用";
		main();

	}

	public void before() throws SQLException, TemplateException, IOException {

		entityName2 = StringUtils.camelCaseWord(tablename);
		entityName = entityName2.substring(0, 1).toUpperCase()
				+ entityName2.substring(1);

		int i = tablename.indexOf("_");

		String s = tablename.substring(i + 1);

		mName = StringUtils.camelCaseWord(s);
		tplPath = URLDecoder.decode(Generator.class.getResource("").getFile(),
				"UTF-8");
		map = this.renderMap();
	}

	public void main() throws Exception {
		before();
		String entityFile = "entity//" + module + "//" + entityName + ".java";
		String mapperFile = "repository//" + module + "//" + entityName
				+ "Mapper.java";
		String mapperxmlFile = "persistence//mysql//" + module + "//"
				+ entityName + "Mapper.xml";
		String serviceFile = "service//" + module + "//" + entityName
				+ "Service.java";
		String controllerFile = "controller//" + module + "//" + entityName
				+ "Controller.java";
		String manageFile = module + "//" + mName + "//" + "manage.jsp";
		String editFile = module + "//" + mName + "//" + "edit.jsp";

		general("entity", entityFile);
		general("mapper", mapperFile);
		general("mapperxml", mapperxmlFile);
		general("service", serviceFile);
		general("controller", controllerFile);
		general("manage", manageFile);
		general("edit", editFile);

	}

	public void general(String type, String fileName) throws TemplateException,
			IOException {
		File source = new File(tplPath + type + ".tpl");

		Template t = new Template(source, map);

		String result = t.render();
		String file = "D://tmp//general//" + tablename + "//" + fileName;
		FileUtils.writeFile(file, result);
	}

	public Map<String, Object> renderMap() throws SQLException,
			TemplateException, IOException {

		// 读取property的属性
		Properties props = new Properties();

		File f = new File(this.getClass().getResource("/").getPath());

		String s = f.getParent() + "/classes/jdbc.properties";

		String s2 = URLDecoder.decode(s, "UTF-8");

		InputStream in = new FileInputStream(s2);

		// 或使用文件输入流(不推荐)，假设当前工作目录为bin
		// InputStream in = new FileInputStream("./config.properties");

		props.load(in);
		in.close();

		// 读取特定属性
		String url = props.getProperty("jdbc.url");
		String username = props.getProperty("jdbc.username");
		String password = props.getProperty("jdbc.password");

		// tplPath =
		// "E:/Workspace/JavaProject/MyEclipse/isperp/target/test-classes/itsm/isperp/codegenerator/entity.tpl";

		DataSourcePool.init(url, username, password);
		DataSourcePool ds = DataSourcePool.getInstance();

		JdbcTemplate jdbctemplate = new JdbcTemplate(ds);

		List<Map<String, Object>> list = getColumns(tablename, dbName,
				jdbctemplate);

		for (Map<String, Object> m : list) {
			m.put("column",
					StringUtils.camelCaseWord((String) m.get("column_name")));
		}
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("created_date",
				new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));

		map.put("tableName", tablename);
		map.put("entityName", entityName);
		map.put("entityName2", entityName2);
		map.put("list", list);
		map.put("mName", mName);

		map.put("module", module);
		map.put("title", title);
		return map;
	}

	private static List<Map<String, Object>> getColumns(String tableName,
			String dbName, JdbcTemplate jdbcTemplate) throws SQLException {

		String sql = "SELECT column_name,column_type,column_comment FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema='"
				+ dbName
				+ "' AND  TABLE_NAME='"
				+ tableName
				+ "' "
				+ " ORDER BY column_name ASC";
		return jdbcTemplate.queryForList(sql);
	}

}
